#include "LinkedStack.h"

LinkedStack* init_stack() {
    LinkedStack* s = (LinkedStack*)malloc(sizeof(LinkedStack));
    s->top = NULL;
    return s;
}

Node* create_node(ElemType data) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = NULL;
}

int is_empty(LinkedStack* stack) { return stack == NULL || stack->top == NULL; }

ElemType top(LinkedStack* stack) {
    if (is_empty(stack)) {
        printf("top: Stack is empty.\n");
        return -1;
    }
    return stack->top->data;
}

void push(LinkedStack* stack, ElemType data) {
    Node* new_node = create_node(data);
    new_node->next = stack->top;
    stack->top = new_node;
}

ElemType pop(LinkedStack* stack) {
    if (is_empty(stack)) {
        printf("pop: Stack is empty.\n");
        return -1;
    }
    ElemType data = stack->top->data;
    Node* temp = stack->top;
    stack->top = stack->top->next;
    free(temp);
    return data;
}

void clear_stack(LinkedStack* stack) {
    while (!is_empty(stack)) {
        pop(stack);
    }
    free(stack);
}

void pop_all(LinkedStack* stack) {
    printf("[TOP >> ");
    while (!is_empty(stack)) {
        printf("%d >> ", pop(stack));
    }
    printf("BOTTOM]\n");
}
